package com.wxxymaker.edu.utils;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @AUTHOR soft
 * @DATE 2018/9/4 17:15
 * @DESCRIBE
 */
public class TimeUtils {

    /**
     * 获取日期
     */
    public static String getDate() {
        return LocalDate.now().format(getDateFormat());
    }

    /**
     * 获取当前时间
     * 格式 yyyy-MM-dd HH:mm:ss
     * @return 时间字符串
     */
    public static String getTime() {
        return LocalDateTime.now().format(getTimeFormat());
    }

    /**
     * 返回当前日期几天后的时间
     * @param afterDay 几天后
     * @return 时间字符串
     */
    public static String getTime(long afterDay) {
        return LocalDateTime.now()
                .plusDays(afterDay)
                .format(getTimeFormat());
    }

    /**
     * 验证一个时间是否过期
     * @param time 时间字符串
     * @return 是否
     */
    public static boolean isOverTime(String time) {
        LocalDateTime now      = LocalDateTime.now();
        LocalDateTime dateTime = LocalDateTime.parse(time, getTimeFormat());
        return dateTime.isBefore(now);
    }

    public static boolean isOverDate(String date) {
        LocalDate now    = LocalDate.now();
        LocalDate target = LocalDate.parse(date, getDateFormat());
        return target.isBefore(now);
    }

    /**
     * 比较两个时间,是否在指定的期限内
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param limit     期限(天)
     * @return 是否
     */
    public static boolean compare(String startTime, String endTime, long limit) {
        limit = limit * 24 * 3600;
        LocalDateTime start   = LocalDateTime.parse(startTime, getTimeFormat());
        LocalDateTime end     = LocalDateTime.parse(endTime, getTimeFormat());
        Duration      between = Duration.between(start, end).abs();
        return between.getSeconds() < limit;
    }

    private static DateTimeFormatter getTimeFormat() {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    }

    private static DateTimeFormatter getDateFormat() {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd");
    }
}
